package com.yaohe.consumer.core;

import java.io.UnsupportedEncodingException;
import java.util.Hashtable;

import com.yaohe.zxing.BarcodeFormat;
import com.yaohe.zxing.MultiFormatWriter;
import com.yaohe.zxing.WriterException;
import com.yaohe.zxing.common.BitMatrix;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.util.Log;

public class GenerateQRBitmap {
	
	private static final int BLACK = 0xFF000000;
	private static final int WHITE = 0xFFFFFFFF;
	
	public static Bitmap gen(String code, int width, int height){
		
		Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
				
		try {
			code = new String(code.getBytes("GBK"), "ISO-8859-1");
			Hashtable hints = new Hashtable();
			try {
				BitMatrix bitMatrix = new MultiFormatWriter().encode(code,
						BarcodeFormat.QR_CODE, width, height, hints);
				
				for(int w = 0; w < width; w++){
					for(int h = 0; h < height; h++){
						bitmap.setPixel(w, h, bitMatrix.get(w, h)?BLACK : WHITE);
					}
				}
				Log.d("qrcode","ok");
				return bitmap;

			} catch (WriterException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.d("qrcode","WriterException:" + e.toString());
			}

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.d("qrcode","UnsupportedEncodingException:" + e.toString());
		}
		return bitmap;
	}

}
